function beta = GMM_Estimator(Stockname, Indexname,lu,ld)

%Calculate return of Stock and Index
Stockname = char(Stockname);
Indexname = char(Indexname);
[Stock Index Date PriceStock PriceIndex] = getStock_Index(Stockname,Indexname);
rStock = PriceStock(2:end)./PriceStock(1:end-1) - 1;
rIndex = PriceIndex(2:end)./PriceIndex(1:end-1) - 1;
%-----------------------------------------------------------

%Create the set of non-limit observations
T = length(rStock);
t = 1;
for i =1:T
    if rStock(i) > ld && rStock(i) < lu
        S(t) = i;
        t = t+1;
    end
end
N = length(S);

%Calculate var of rIndex, cov(rStock, rIndex) , beta
sigma2 = 0;
cov = 0;
for j=2:N
    %rStock, rIndex co lay log ko
    temp = log(PriceIndex(S(j))/PriceIndex(S(j-1)))-(S(j)-S(j-1))*mean(log(rIndex+1));
    sigma2 = sigma2 + (1/T)*temp^2;
    cov = cov + (1/T)*temp*(log(PriceStock(S(j))/PriceStock(S(j-1)))-(S(j)-S(j-1))*mean(log(rStock+1)));
end
beta = cov/sigma2;
